<?php
class ChartAction extends ZMAction {
    public function index(){
        $start = $_REQUEST['start'];
        $limit = $_REQUEST["limit"];
        $sortArr = json_decode(str_replace("\\\"","\"",$_REQUEST['sort']),true);
        if($sortArr){
            $sort = "";
            foreach($sortArr as $item){
                if($sort != '') $sort .= ',';
                $sort .= $item['property']. ' ' . $item['direction'];
            }
        }
        if(!is_numeric($start)){
            $start = 0;
        }
        if(!is_numeric($limit)){
            $limit = 20;
        }
        $Model = M("Chart");
        $count = $Model->count();
        if(empty($sort)){
            $result = $Model->limit("$start,$limit")->select();
        }else{
            $result = $Model->order("$sort $dir")->limit("$start,$limit")->select();
        }
        if($result == null){
            $result = array();
        }
        $this->ajaxRetArray($result,$count);    
    }
    public function save(){
        $model = M('Chart');
        $tid = $_REQUEST['cid'];
        
        $data = $model->create();
        
        if(empty($tid)){
            $tid = $model->add($data);
            if($ddid===false){
                $this->error('保存错误!');
                return;
            }
        }else{
            $model->save($data);
        }
                
        $s = str_replace("\\\"","\"",$_REQUEST['items']);
        $s = str_replace("\\\\","\\",$s);
        $itemArr = json_decode($s,true);

        $modelItems = M('ChartItems');
        $modelItems->where("cid=$tid")->delete();
        
        foreach($itemArr as $row){
            unset($data);
            $data['cid'] = $tid;
            $data['dname'] = $row['dname'];
            $data['did'] = $row['did'];
            $modelItems->add($data);
        }
        
        $this->success('ok');
    }
    public function del(){
        $tid = $_REQUEST['cid'];
        $model = M('Chart');
        $model->where("cid=$tid")->delete();
        
        $modelItems = M('ChartItems');
        $modelItems->where("cid=$tid")->delete();
        
        $this->success('ok');
    }
    
    public function getItems(){
        $tid = $_REQUEST['tid'];
        
        $modelItems = M('ChartItems');
        $modelYc = M('Yc');
        $res = $modelItems->where("cid=$tid")->order('id')->select();
        if($res){
            foreach($res as &$row){
                $did = $row['did'];
                $resYc = $modelYc->where("id=$did")->find();
                if($resYc){
                    $row['desc'] = '[' . $resYc['sname'] . ']' .$resYc['dname'];
                }
            }
        }else{
            $res = array();
        }
        $this->ajaxRetArray($res);
    }
    
    public function getEnum(){
        $model = M('Chart');
        $res = $model->field('cid,chartname')->select();
        foreach($res as &$row){
            $row['id'] = $row['cid'];
            $row['text'] = $row['chartname'];
        }
        $this->ajaxRetArray($res);
    }
    
    public function getChart(){
        $cid = $_REQUEST['cid'];
        $model = M('Chart');
        $res = $model->where("cid=$cid")->find();
        if($res){
            $ret = array();
            $ret['chartname'] = $res['chartname'];
            $modelItems = M('ChartItems');
            $resItem = $modelItems->where("cid=$cid")->select();
            $fields = array();
            $fields[] = array('name'=>'ptime','type'=>'date');
            $i = 0;
            $lfields = array();
            $series = array();
            foreach($resItem as $row){
                $col = array();
                $col['name'] = "d".$row['did'];
                $col['type'] = 'float';
                $fields[] = $col;
                
                $lfields[] = "d".$row['did'];
                
                $ser = array();
                $ser['type'] = 'line';
                $ser['xField'] = 'ptime';
                $ser['yField'] = "d".$row['did'];
                $ser['smooth'] = true;
                $ser['title'] = $row['dname'];
                $ser['showMarkers'] = false;
                
                $series[] = $ser;
                
                $i++;
            }
            $ret['fields'] = $fields;
            $ret['lfields'] = $lfields;
            $ret['series'] = $series;
            
            $this->ajaxReturn($ret,'',true);
        }else{
            $this->error('error');
        }
    }
    
    public function getChartData(){
        $startdate = $_REQUEST['startdate'];
        if(empty($startdate)){
            $startdate = date('Y-').'01-01';
        }
        $enddate = $_REQUEST['enddate'];
        if(empty($enddate)){
            $enddate = date('Y-m-d');
        }
        $cid = $_REQUEST['cid'];
        $modelItems = M('ChartItems');
        $resItem = $modelItems->where("cid=$cid")->select();
        $enddate = date('Y-m-d',strtotime('+1 day',strtotime($enddate)));
        $where['_string'] = "ptime>='$startdate' and ptime<'$enddate'";      
        
        $ret = array();
        foreach($resItem as $row){
            $did = $row['did'];
            $modYc = M("YcH$did");
            $resData = $modYc->where($where)->select();
            $ret[] = $resData;
        }
        $retArr = array();
        
        $i = 0;
        foreach($ret[0] as $res){
            $data = array();
            $data['ptime'] = $res['ptime'];
            $j=0;
            foreach($resItem as $row){
                $colname = "d" . $row['did'];
                $data[$colname] = $ret[$j][$i]['val'];
                $j++;
            }
            $retArr[] = $data;
            $i++;
        }
        
        
        $this->ajaxRetArray($retArr);
    }
}